/*
 * Copyright (C) 2008  Marcelo Busico <marcelobusico@simbya.com.ar>
 * 
 * This file is part of a SIMBYA project.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package gecom.importer;

import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 * Ventana principal del importador.
 * @author Marcelo Busico.
 */
public class VentanaPrincipal extends javax.swing.JFrame {

    private static final Logger log = Logger.getLogger(VentanaPrincipal.class);

    /** Creates new form VentanaPrincipal */
    public VentanaPrincipal() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSalir = new javax.swing.JButton();
        lblOrigen = new javax.swing.JLabel();
        txtOrigenODBC = new javax.swing.JTextField();
        btnComenzar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        lblEjemplo = new javax.swing.JLabel();
        txtServer = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtPuerto = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Importador Productos: SGS a SIMBYA-GECOM");

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        lblOrigen.setText("Origen ODBC");

        txtOrigenODBC.setText("sgsdb");

        btnComenzar.setText("Comenzar importación");
        btnComenzar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnComenzarActionPerformed(evt);
            }
        });

        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("<html>\n<h1>Advertencia</h1>\n<p>\nEste programa debe ser ejecutado solo una vez para obtener todos los productos cargados en \"Sistema de Gestión de Stock\" e importarlos en \"SIMBYA-GECOM\".<br/>\nSi existe algún producto en el destino con el mismo código que en el origen, será ignorado ese producto para la importación y se conservará el producto del destino.\n</p>\n</html>");

        lblEjemplo.setFont(new java.awt.Font("Dialog", 0, 12));
        lblEjemplo.setText("No incluir jdbc:odbc en el nombre");

        txtServer.setText("10.0.0.10");

        jLabel2.setText("Dirección Destino");

        jLabel3.setText("Puerto");

        txtPuerto.setText("22869");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(lblOrigen))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblEjemplo)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtServer, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtPuerto, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtOrigenODBC)))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSalir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnComenzar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblOrigen)
                    .addComponent(txtOrigenODBC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblEjemplo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(txtPuerto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 74, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnComenzar))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-430)/2, (screenSize.height-325)/2, 430, 325);
    }// </editor-fold>//GEN-END:initComponents

private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
    System.exit(0);
}//GEN-LAST:event_btnSalirActionPerformed

private void btnComenzarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnComenzarActionPerformed
    if (txtOrigenODBC.getText() == null || txtOrigenODBC.getText().isEmpty()) {
        JOptionPane.showMessageDialog(this, "Debe ingresar el origen de datos");
        return;
    }
    if (txtServer.getText() == null || txtServer.getText().isEmpty()) {
        JOptionPane.showMessageDialog(this, "Debe ingresar la dirección del servidor");
        return;
    }
    if (txtPuerto.getText() == null || txtPuerto.getText().isEmpty()) {
        JOptionPane.showMessageDialog(this, "Debe ingresar el puerto del servidor");
        return;
    }
    int res = JOptionPane.showConfirmDialog(this, "Comenzará el proceso" +
            " de importación. ¿Está seguro?", "Confirme", JOptionPane.YES_NO_OPTION);
    if (res == JOptionPane.NO_OPTION) {
        return;
    }
    try {
        ImportadorSGS.importar("jdbc:odbc:" + txtOrigenODBC.getText(),
                txtServer.getText(), Integer.valueOf(txtPuerto.getText()));
    } catch (Exception ex) {
        log.error("Error al importar.", ex);
        JOptionPane.showMessageDialog(this, ex.getMessage());
        System.exit(0);
    }
    JOptionPane.showMessageDialog(this,"Proceso de importacion finalizado con éxito.");//GEN-LAST:event_btnComenzarActionPerformed
    System.exit(0);
}                                           

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaPrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnComenzar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lblEjemplo;
    private javax.swing.JLabel lblOrigen;
    private javax.swing.JTextField txtOrigenODBC;
    private javax.swing.JTextField txtPuerto;
    private javax.swing.JTextField txtServer;
    // End of variables declaration//GEN-END:variables

}
